package com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.PersonnelManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.Role;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.service.PersonnelManagementService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.vo.SysUserImportVo;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import javax.validation.Valid;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/10
 * @Description: 现场人员管理
 */
@RestController
@RequestMapping("/personnelManagement")
@Slf4j
@Api(tags ="现场人员管理")
public class PersonnelManagementController {
    @Resource
    private PersonnelManagementService personnelManagementService;

    /**
     * 新增单条数据
     * @param param
     * @return
     */
    @PostMapping("save")
    @Operation(summary = "新增数据")
    public RestResponse save(@RequestBody  PersonnelManagement param) {
        RestResponse restResponse=null;
        try {
            personnelManagementService.save(param);
            restResponse=RestResponse.succeed();
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "保存失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改单条数据
     * @param param
     * @return
     */
    @PostMapping("updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody  PersonnelManagement param) {
        try {
            personnelManagementService.updateById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /**
     * 删除
     * @param Id
     * @return
     */
    @PostMapping("deleteById/{id}")
    public RestResponse deleteById(@PathVariable("id") List<String> ids) {
        RestResponse response = null;
        try {
            personnelManagementService.deleteById(ids);
            response= RestResponse.succeed();
            response.setMessage("删除成功");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 解析excel
     * @param file
     * @param xsemId
     * @return
     */
    @PostMapping("parseExcel")
    public RestResponse parseExcel(@RequestParam("file") MultipartFile file, String xsemId) {
        RestResponse restResponse=null;
        try {
            personnelManagementService.importExcel(file,xsemId);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("导入成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "导入失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分页列表
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(PersonnelManagement queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=personnelManagementService.getPage(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("getHeadInfo")
    @Operation(summary = "获取用户名称与创建时间")
    public RestResponse getHeadInfo() {
        try {
            PersonnelManagement headInfo = personnelManagementService.getHeadInfo();
            return RestResponse.succeed(headInfo);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    @GetMapping("findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            PersonnelManagement data = personnelManagementService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    @GetMapping("getRole")
    @Operation(summary = "获取全部角色")
    public RestResponse getRole() {
        try {
            List<Role> data = personnelManagementService.getRole();
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 系统用户导入
     * @return
     */
    @PostMapping("/sysUserImport")
    public RestResponse sysUserImport(@RequestBody SysUserImportVo sysUserImportVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            personnelManagementService.sysUserImport(sysUserImportVo);
            restResponse = RestResponse.succeed();
            restResponse.setMessage("导入成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "导入失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
